The `array_merge` function in PHP is a powerful tool for combining multiple arrays into one. This function takes an arbitrary number of arrays as parameters and merges them into a single array. The way `array_merge` handles keys, values, and nested arrays is crucial to understanding its behavior. Let’s delve into its functionality with examples and reference some reliable sources to explain this comprehensively.
The `array_merge` function concatenates the input arrays. If the input arrays have identical string keys, then the last value for that key will overwrite the previous one. However, if the arrays contain numeric keys, `array_merge` will reindex them, starting from zero, in the resulting array.
```
array array_merge(array …$arrays);
```
Here, `…$arrays` denotes that the function can take a variable number of arrays as arguments.
Consider two associative arrays with some overlapping keys:
```
$array1 = [“color” => “red”, “shape” => “circle”];
$array2 = [“color” => “blue”, “size” => “large”];
$result = array_merge($array1, $array2);
print_r($result);
```
Output:
```
Array
(
[color] => blue
[shape] => circle
[size] => large
)
```
In this example, the value of the “color” key from `$array2` overwrites the value from `$array1`.
Let’s consider arrays with numeric keys:
```
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$result = array_merge($array1, $array2);
print_r($result);
```
Output:
```
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
```
In this case, the numeric keys are reindexed in the resulting array.
Combining arrays with both string and numeric keys:
```
$array1 = [“a” => “apple”, “b” => “banana”];
$array2 = [“a” => “apricot”, 20 => “berry”];
$result = array_merge($array1, $array2);
print_r($result);
```
Output:
```
Array
(
[a] => apricot
[b] => banana
[0] => berry
)
```
Here, the string key “a” in `$array2` overwrites the “a” in `$array1`, and the numeric key `20` from `$array2` is reindexed to `0` in the result.
You can merge multiple arrays at once:
```
$array1 = [“a” => “apple”];
$array2 = [“b” => “banana”];
$array3 = [“c” => “cherry”];
$result = array_merge($array1, $array2, $array3);
print_r($result);
```
Output:
```
Array
(
[a] => apple
[b] => banana
© => cherry
)
```
1. PHP Manual: The official PHP documentation provides detailed information about `array_merge` and its behavior.
- Source: [PHP: array\_merge – Manual](https://www.php.net/manual/en/function.array-merge.php)
1. W3Schools: A well-recognized source for web development tutorials and reference materials that explain how `array_merge` is used in various scenarios.
- Source: [W3Schools – PHP array_merge() Function](https://www.w3schools.com/php/func_array\_merge.asp)
1. Geeks for Geeks: Offers insightful explanations and examples about various PHP functions, including array manipulation functions.
- Source: [Geeks for Geeks – PHP array_merge() Function](https://www.geeksforgeeks.org/php-array_merge-function/)
The `array_merge` function is indispensable when working with arrays in PHP, particularly when you need to concatenate arrays efficiently while handling keys carefully. This function’s ability to handle string and numeric keys differently, along with its capacity to merge multiple arrays at once, provides great flexibility. Understanding its key behaviors ensures that you can leverage its full potential in your PHP projects.